<template>
<div class="container tree-list-layout">
  <treeOffice @click="handleTreeNodeClick" ref="treeOffice"  class="box-left"/>
  <data-view
    :propIsTdesign="true"
    class="box-right"
    ref="dataView"
    propViewKey="base_dept"
    propViewForm="base/dept/detail.vue"
    @extra-button-click="handleExtraButtonClick"
    :propInitData="selectedTreeNode"
  />

  <el-drawer title="科室耗材范围" size="75%" :append-to-body="false" direction="rtl" :visible.sync="dialogDeptMatrVisible" v-dialog-resize>
    <data-view ref="dvLimitMatr"
               propViewKey="dept_limit_matr"
               :propInitData="deptInitData"
               @extra-button-click="extraButtonClick"
               :propPreQueryWrapper="$newWrapper().eq('deptId', this.deptInitData.id)"
    />
  </el-drawer>

  <el-dialog title="耗材选择" :visible.sync="matrDialogVisible" v-dialogDrag1 v-dialog-resize width="80%" height="50vh" center :destroy-on-close="true">
    <matr-select @confirm-check="confirmCheck">
    </matr-select>
  </el-dialog>

  <el-drawer title="设置科室库位权限"
             :append-to-body="true"
             :visible.sync = "setWarehouseVisible"
             direction="rtl"
             size="50%">
    <UserWareHouse ref="warehouseForm"
                   :propInitData="warehouseInitData"
                   :propSetTypeField="'dept_id'"
                   @formClose="setWarehouseVisible = false"
    />
  </el-drawer>
</div>
</template>

<script>
import deptMatr from '@/views/dept/limitmatr/detail.vue'
import matrSelect from '@/components/base/popover/MatrSelect.vue'
import workbenchDialog from '@/views/sys/workbench/index.vue'
import UserWareHouse from '@/views/sys/user-warehouse/detail.vue'
import treeOffice from '@/components/custom/bus/treeOffice.vue'

export default {
  name: 'dept',
  data() {
    return {
      treeData: [],
      dialogDeptMatrVisible: false,
      matrDialogVisible: false,
      drawer: false,
      selectedTreeNode: {},
      deptInitData: {},
      multipleDept: {},
      warehouseInitData: {},
      setWarehouseVisible: false
    }
  },
  computed() {},
  created() {
  },
  components: {
    deptMatr,
    matrSelect,
    workbenchDialog,
    UserWareHouse,
    treeOffice
  },
  watch: {
  },
  methods: {

    handleExtraButtonClick(para) {
      if (para.button.key == 'sync-office') {
        this.$ajax.post('base/dept/syncOffice').then(response => {
          if (response.code == 0) {
            this.utilMessage.success(`数据同步成功，影响记录数量:${response.data}`)
            this.$refs.dataView.ocRefresh()
          }
        })
      } else if (para.button.key === 'addCommonMatr') {
        this.multipleDeptData = {}
        if (!para.data || para.data.length < 2) {
          this.utilMessage.error('至少选择两个以上科室')
          return
        }
        this.matrDialogVisible = true
        this.multipleDeptData = para.data
      } else if (para.button.key === 'setMatrScope') {
        this.multipleDeptData = {}
        this.dialogDeptMatrVisible = true
        this.deptInitData = para.data
        this.$nextTick(() => {
          let wrapper = this.$newWrapper().eq('deptId', this.deptInitData.id)
          this.$refs.dvLimitMatr.setQueryWrapper(wrapper)
        })
      } else if (para.button.key === 'setWarehouse') { //设置科室仓库权限
        this.warehouseInitData = para.data
        this.setWarehouseVisible = true
        this.$nextTick(() => {
          this.$refs.warehouseForm.initData()
        })
      } else if (para.button.key === 'sync-hrp') { //同步耗材
        this.$ajax.get('hrp/get_hrp_dept').then(response => {
          if (response.code == 0) {
            this.utilMessage.success(`${response.msg}`)
            this.$refs.dataView.ocRefresh()
          }
        })
      }
    },

    //扩展加载数据后操作
    handleAfterLoad() {},
    extraButtonClick({
      button,
      data,
      index
    }) {
      if (button.key === 'closeForm') {
        this.dialogDeptMatrVisible = false
      }
      let checkedRow = data.records
      if (button.key === 'updateDemand') {
        const selectedMatrArray = []
        checkedRow.forEach(item => {
          let demand
          if (item.isDemand == 1) {
            demand = 0
          } else {
            demand = 1
          }
          let params = {
            deptId: item.deptId,
            deptName: item.deptName,
            matrId: item.matrId,
            id: item.id,
            isApply: item.isApply,
            isDemand: demand,
          }
          selectedMatrArray.push(params)
        })
        this.$ajax.post('/dept/limit_matr/saveBatch', selectedMatrArray).then(response => {
          response.success && this.utilMessage.success(response.msg)
          this.matrDialogVisible = false
          let wrapper = this.$newWrapper().eq('deptId', this.deptInitData.id)
          this.$refs.dvLimitMatr.setQueryWrapper(wrapper)
        })
      }
      if (button.key === 'updateApply') {
        const selectedMatrArray = []
        checkedRow.forEach(item => {
          let apply
          if (item.isApply == 1) {
            apply = 0
          } else {
            apply = 1
          }
          let params = {
            deptId: item.deptId,
            deptName: item.deptName,
            matrId: item.matrId,
            id: item.id,
            isApply: apply,
            isDemand: item.isDemand,
          }
          selectedMatrArray.push(params)
        })
        this.$ajax.post('/dept/limit_matr/saveBatch', selectedMatrArray).then(response => {
          response.success && this.utilMessage.success(response.msg)
          this.matrDialogVisible = false
          let wrapper = this.$newWrapper().eq('deptId', this.deptInitData.id)
          this.$refs.dvLimitMatr.setQueryWrapper(wrapper)
        })
      }
      // eslint-disable-next-line no-empty
      if (button.key === 'addDeptMatr') {
        this.matrDialogVisible = true
      }
    },
    confirmCheck(rows) {
      const selectedMatrArray = []
      if (this.multipleDeptData.length > 1) {
        this.multipleDeptData.forEach(item => {
          rows.forEach(row => {
            let params = {
              deptId: item.id,
              deptName: item.name,
              matrId: row.id
            }
            selectedMatrArray.push(params)
          })
        })
      } else {
        rows.forEach(row => {
          let params = {
            deptId: this.deptInitData.id,
            deptName: this.deptInitData.name,
            matrId: row.id
          }
          selectedMatrArray.push(params)
        })
      }

      this.$ajax.post('/dept/limit_matr/saveBatch', selectedMatrArray).then(response => {
        response.success && this.utilMessage.success(response.msg)
        this.matrDialogVisible = false
        let wrapper = this.$newWrapper().eq('deptId', this.deptInitData.id)
        this.$refs.dvLimitMatr.setQueryWrapper(wrapper)
      })
    },
    handleTreeNodeClick(data) {
      this.selectedTreeNode = data
      let wrapper = data.id == 0 ? this.$newWrapper() : this.$newWrapper().eq('office_id', data.id)
      this.$refs.dataView.setQueryWrapper(wrapper)
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  height: 100%;
  background-color: #fff;
}

.tree-container {
  padding: 10px;
  margin-right: 20px;
  border: solid 1px #DCDFE6;
  height: 100%
}

.tree-filter {
  margin: 10px;
  width: 80%
}
</style>
